package net.arvin.mobilebanksystem.fragment;

import java.util.List;

import net.arvin.dao.ResponseHandler;
import net.arvin.dao.StickListTaskAdapter;
import net.arvin.mobilebanksystem.R;
import net.arvin.model.Bills;
import net.arvin.service.BillsService;
import net.arvin.utils.NetConnectionUtil;
import net.arvin.utils.PreferenceUtils;
import se.emilsjolander.stickylistheaders.StickyListHeadersListView;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;

public class BillsFragment extends Fragment implements OnItemClickListener,
		OnItemLongClickListener {
	private StickyListHeadersListView billsView;
	private StickListTaskAdapter stickListTaskAdapter;
	private List<Bills> bills;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_bills, null);
		init(view);
		return view;
	}

	private void init(View view) {
		initView(view);
		initDate();
		resetData();
	}

	private void initDate() {
		String billsStr = PreferenceUtils.getPreferBillsJson(getActivity());
		if (billsStr != null) {
			bills = JSON.parseArray(billsStr, Bills.class);
			initListener();
		}
	}

	private void initListener() {
		stickListTaskAdapter = new StickListTaskAdapter(getActivity(), bills);
		billsView.setAdapter(stickListTaskAdapter);

		billsView.setDrawingListUnderStickyHeader(true);
		billsView.setAreHeadersSticky(true);
		billsView.setOnItemLongClickListener(this);
		billsView.setOnItemClickListener(this);
	}

	private void resetData() {
		if (NetConnectionUtil.isConnectWithTip(getActivity(), "没有联网=_=!")) {
			BillsService.tryGetAllBills(
					PreferenceUtils.getPreferPhonenumber(getActivity()),
					getResponseHandler(), getActivity());
		}
	}

	private void initView(View view) {
		billsView = (StickyListHeadersListView) view.findViewById(R.id.bills);
	}

	private ResponseHandler getResponseHandler() {
		{
			ResponseHandler mResponseHandler = new ResponseHandler() {
				@Override
				public void onRequsetSucceeded(final Object data) {
					bills = JSON.parseArray(data.toString(), Bills.class);
					PreferenceUtils.modifyStringValueInPreferences(
							PreferenceUtils.BillsJSON, data.toString(),
							getActivity());
					initListener();
				}

				@Override
				public void onRequestFailed(final Object data) {
					Toast.makeText(getActivity(), "没有账单~", Toast.LENGTH_SHORT)
							.show();
				}
			};
			return mResponseHandler;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		Toast.makeText(getActivity(), "" + position, Toast.LENGTH_SHORT).show();
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view,
			int position, long id) {
		return false;
	}
}
